{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Machine Learning UIs with Gradio.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyM49BYqbrzyd9ZFYDaAULId"
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "73elJnlQg0o_"
      },
      "source": [
        "# Temporary POC deployment of ML workflows : Using Gradio\n",
        "\n",
        "> Divyanshu Vyas | Data Scientist "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zvYbCLU8H1n5"
      },
      "source": [
        "# !pip install gradio"
      ],
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "riUiGaxLhdWD"
      },
      "source": [
        "# 1. Understanding how to run it. "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rRpLG_NlIGcg"
      },
      "source": [
        "import gradio as gr"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 35
        },
        "id": "w2q3IQ3YKzUp",
        "outputId": "5e66da2e-b9d4-4514-a9f2-917ccb785c70"
      },
      "source": [
        "def squarer(x):\n",
        "  return f'The Square is = {x**2}'\n",
        "\n",
        "squarer(2)"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "application/vnd.google.colaboratory.intrinsic+json": {
              "type": "string"
            },
            "text/plain": [
              "'The Square is = 4'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ejmrXwE_XSjI"
      },
      "source": [
        "# gr.Interface(fn=squarer, inputs=\"number\", outputs=\"text\").launch()"
      ],
      "execution_count": 10,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mOc2LJ31hhV9"
      },
      "source": [
        "# 2. ML (Regression) Project Deployment."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uQ1C2KsaY-XA"
      },
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import seaborn as sns\n",
        "import matplotlib.pyplot as plt\n"
      ],
      "execution_count": 12,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nWxEX1tdXY0-"
      },
      "source": [
        "df = pd.read_csv('/content/sample_data/california_housing_train.csv')"
      ],
      "execution_count": 18,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "cDso8OkkY9FU",
        "outputId": "f9df11ea-e0b9-4213-c4ab-05e76e639c86"
      },
      "source": [
        "df.shape"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(17000, 9)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "i_4VpNyKZF6Y",
        "outputId": "75752776-1714-46bf-acf0-89c7d916723e"
      },
      "source": [
        "df.columns"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['longitude', 'latitude', 'housing_median_age', 'total_rooms',\n",
              "       'total_bedrooms', 'population', 'households', 'median_income',\n",
              "       'median_house_value'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 21
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4iU5x2tfZkHe"
      },
      "source": [
        "df = df.iloc[:,2:]"
      ],
      "execution_count": 23,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tDT_fgfzaT8R",
        "outputId": "499e84a8-e3da-4029-cf51-a4aed8a4040b"
      },
      "source": [
        "df.columns"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['housing_median_age', 'total_rooms', 'total_bedrooms', 'population',\n",
              "       'households', 'median_income', 'median_house_value'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "5o8hIXr2a0Mg",
        "outputId": "b744c7b8-c34b-4fe0-88f0-adde228fb842"
      },
      "source": [
        "df.shape"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(17000, 7)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DynU1UZea2q5"
      },
      "source": [
        "from sklearn.model_selection import train_test_split"
      ],
      "execution_count": 26,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "njFRjD-ObJko"
      },
      "source": [
        "X = df.iloc[:,:-1]\n",
        "y = df.iloc[:,-1]"
      ],
      "execution_count": 28,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NW5QrgG-a9vO"
      },
      "source": [
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=101)"
      ],
      "execution_count": 29,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZZF_OzePbIIP"
      },
      "source": [
        "from sklearn.preprocessing import StandardScaler, MinMaxScaler"
      ],
      "execution_count": 46,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GY2CWzYjbR0x"
      },
      "source": [
        "scaler = MinMaxScaler()"
      ],
      "execution_count": 47,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FG8Gq_KebTSf",
        "outputId": "a382e54d-57c7-4df1-dffc-fd229f1c6312"
      },
      "source": [
        "X.columns"
      ],
      "execution_count": 48,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['housing_median_age', 'total_rooms', 'total_bedrooms', 'population',\n",
              "       'households', 'median_income'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 48
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AvYKGRmqbVBk"
      },
      "source": [
        "X_train_scaled = scaler.fit_transform(X_train)\n",
        "X_test_scaled = scaler.transform(X_test)"
      ],
      "execution_count": 49,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bK2Z3l1Tbesq"
      },
      "source": [
        "from sklearn.linear_model import LinearRegression"
      ],
      "execution_count": 50,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hjoeAeCtb5Zy"
      },
      "source": [
        "model = LinearRegression()"
      ],
      "execution_count": 51,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "m2d81Unpb8CB",
        "outputId": "68148e27-3f37-4960-f427-243f7bc4d66e"
      },
      "source": [
        "model.fit(X_train_scaled, y_train)"
      ],
      "execution_count": 52,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 52
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NW1EF2Tmb_o1"
      },
      "source": [
        "yp = model.predict(X_test_scaled)"
      ],
      "execution_count": 53,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pQXe8RV_cD-Q"
      },
      "source": [
        "from sklearn.metrics import mean_squared_error as mse"
      ],
      "execution_count": 54,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "skZ68UJvcKj2"
      },
      "source": [
        "rmse = np.sqrt(mse(y_test,yp))"
      ],
      "execution_count": 55,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Si5oB-N8cRj3",
        "outputId": "a82350f3-c6ac-4c66-abf3-03768b7a5803"
      },
      "source": [
        "rmse*100/np.mean(y_test)"
      ],
      "execution_count": 56,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "35.60010349954132"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 56
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "89DGScSbcmPF",
        "outputId": "efa36e50-9fac-4e72-a650-95c7200590fe"
      },
      "source": [
        "X_test.columns"
      ],
      "execution_count": 57,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['housing_median_age', 'total_rooms', 'total_bedrooms', 'population',\n",
              "       'households', 'median_income'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 57
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9BVt9GVwcwR-",
        "outputId": "0b4fd0ab-ae4c-493f-ad61-e862beea9c0b"
      },
      "source": [
        "model.predict([X_test_scaled[0,:]])"
      ],
      "execution_count": 62,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([279531.19427309])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 62
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_ZXy6c5IhmlK"
      },
      "source": [
        "## Central Function is to be defined based on which code becomes production ready."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HCkp2L0-cSSn"
      },
      "source": [
        "def housepricer(median_age, total_rooms_in_locality, total_bedrooms, population, households, median_income):\n",
        "\n",
        "  '''\n",
        "  housing_median_age        28.589353\n",
        "total_rooms             2643.664412\n",
        "total_bedrooms           539.410824\n",
        "population              1429.573941\n",
        "households               501.221941\n",
        "median_income              3.883578\n",
        "median_house_value    207300.912353\n",
        "  '''\n",
        "\n",
        "  X_new = [np.array([median_age, total_rooms_in_locality, total_bedrooms, population, households, median_income])]\n",
        "\n",
        "  X_new_scaled = scaler.transform(X_new)\n",
        "\n",
        "  yp_new = model.predict(X_new_scaled)\n",
        "\n",
        "  P = np.round(yp_new[0],2)\n",
        "\n",
        "  out = f'Median House Value = {P} USD'\n",
        "\n",
        "  return out\n",
        "\n"
      ],
      "execution_count": 79,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kNpNYuImhsqb"
      },
      "source": [
        "# Finally, run the app Interface."
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 883
        },
        "id": "mrQsfxTCdVQb",
        "outputId": "69a1aea6-bf29-47ea-c0db-6aa8af627fe5"
      },
      "source": [
        "gr.Interface(fn=housepricer, inputs=[\"number\",\"number\",\"number\",\"number\",\"number\",\"number\"], outputs=\"text\" , title='House Price Predictor').launch(debug=True)"
      ],
      "execution_count": 80,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
            "This share link will expire in 24 hours. If you need a permanent link, visit: https://gradio.app/introducing-hosted (NEW!)\n",
            "Running on External URL: https://39494.gradio.app\n",
            "Interface loading below...\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "display_data",
          "data": {
            "text/html": [
              "\n",
              "        <iframe\n",
              "            width=\"900\"\n",
              "            height=\"500\"\n",
              "            src=\"https://39494.gradio.app\"\n",
              "            frameborder=\"0\"\n",
              "            allowfullscreen\n",
              "        ></iframe>\n",
              "        "
            ],
            "text/plain": [
              "<IPython.lib.display.IFrame at 0x7f40f8cfff50>"
            ]
          },
          "metadata": {
            "tags": []
          }
        },
        {
          "output_type": "error",
          "ename": "KeyboardInterrupt",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-80-e7d9c9cfa4c7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mgr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mInterface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhousepricer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"number\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"number\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"number\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"number\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"number\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"number\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"text\"\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0mtitle\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'House Price Predictor'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlaunch\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdebug\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;32m/usr/local/lib/python3.7/dist-packages/gradio/interface.py\u001b[0m in \u001b[0;36mlaunch\u001b[0;34m(self, inline, inbrowser, share, debug, auth, auth_message, private_endpoint, prevent_thread_lock)\u001b[0m\n\u001b[1;32m    540\u001b[0m             \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    541\u001b[0m                 \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstdout\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflush\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 542\u001b[0;31m                 \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    543\u001b[0m         \u001b[0mis_in_interactive_mode\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msys\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ps1'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mflags\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minteractive\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    544\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mprevent_thread_lock\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mis_in_interactive_mode\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
            "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0tKZKhShdYL4"
      },
      "source": [
        ""
      ],
      "execution_count": 80,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pqqQUBDGdaJ2"
      },
      "source": [
        ""
      ],
      "execution_count": 80,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nyEFT7gydtr6"
      },
      "source": [
        ""
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}